import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"

// https://vitejs.dev/config/
export default defineConfig((command, mode) => {
  // const env = loadEnv(mode, process.cwd(), '')
  // console.log('----command', command)
  // { mode: 'development', command: 'serve' }-{ mode: 'production', command: 'build' }--{ mode: 'qa', command: 'build' }
  let base = '/qa/testVite/'
  switch (command.mode) {
    case 'development':
    case 'qa':
      base = '/qa/testVite/'
      break;
    case 'production':
      base = '/pro/testVite/'
      break;
  }
  return {
    base,
    plugins: [vue()],
    resolve: {
      alias: {
        "@": path.join(__dirname, "src"),
      }
    },
    server: {
      host: '0.0.0.0',
      hmr: true
    },
    css: {
      sourceMap: false,
      modules: false,
      postcss: {
        plugins: [
          // require('autoprefixer'),
          require('postcss-pxtorem')({ // 把px单位换算成rem单位
            rootValue: 37.5, // vant官方使用的是37.5, 对根元素大小进行设置。类似px2rem中的remUnit参数
            unitPrecision: 8,
            minPixelValue: 2, // 所有小于2px的样式都不被转换
            // selectorBlackList: ['vant', 'mu'], // 忽略转换正则匹配项
            propList: ['*'] // 存储哪些将被转换的属性列表
          })
        ]
      }
    },
    build: {
      minify: 'terser',
      brotliSize: false,
      manifest: true,
      outDir: command.mode === 'production' ? 'pro' : 'qa',
      sourcemap: command.mode !== 'production',
      cssCodeSplit: true,
      emptyOutDir: true,
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: false
          // drop_console: Boolean(VITE_DROP_CONSOLE)
        }
      }
    }
  }
})
